;------------------------------------------------------------------------
;- Written by: Bazz
;-    This code introduces the programmer to the VBLANK interrupt, and
;-    teaches him/her its powers. This should be pretty simple & easy
;-    to go through.
;------------------------------------------------------------------------

;== Include MemoryMap, HeaderInfo, and interrupt Vector table ==
.INCLUDE "header.inc"

;== Include Library Routines ==
.INCLUDE "InitSNES.asm"
.INCLUDE "LoadGraphics.asm"

;== EQUates ==
.EQU PalNum $0000       ; Use some RAM

;==========================================
; Main Code
;==========================================

.MACRO Stall
    .REPT 7
        WAI
    .ENDR
.ENDM

.BANK 0 SLOT 0
.ORG 0
.SECTION "MainCode"

.define INIDISP 	$2100
.define OBSEL 		$2101
.define OAMADDL 	$2102
.define OAMADDH 	$2103
.define OAMDATA 	$2104
.define BGMODE 		$2105
.define MOSAIC 		$2106
.define BG1SC 		$2107
.define BG2SC 		$2108
.define BG3SC 		$2109
.define BG4SC 		$210a
.define BG12NBA 	$210b
.define BG34NBA 	$210c
.define BG1HOFS 	$210d
.define M7HOFS 		$210d
.define BG1VOFS 	$210e
.define M7VOFS 		$210e
.define BG2HOFS 	$210f
.define BG2VOFS	 	$2110
.define BG3HOFS	 	$2111
.define BG3VOFS	 	$2112
.define BG4HOFS	 	$2113
.define BG4VOFS	 	$2114
.define VMAIN		$2115
.define VMADDL		$2116
.define VMADDH		$2117
.define VMDATAL		$2118
.define VMDATAH		$2119
.define M7SEL		$211a
.define M7A		$211b
.define M7B		$211c
.define M7C		$211d
.define M7D		$211e
.define M7X		$211f
.define M7Y		$2120
.define CGADD		$2121
.define CGDATA		$2122
.define W12SEL		$2123
.define W34SEL		$2124
.define WOBJSEL		$2125
.define WH0		$2126
.define WH1		$2127
.define WH2		$2128
.define WH3		$2129
.define WBGLOG		$212a
.define WOBJLOG		$212b
.define TM		$212c
.define TS		$212d
.define TMW		$212e
.define TSW		$212f
.define CGWSEL		$2130
.define CGADSUB		$2131
.define COLDATA		$2132
.define SETINI		$2133
.define MPYL		$2134
.define MPYM		$2135
.define MPYH		$2136
.define SLHV		$2137
.define OAMDATAREAD	$2138
.define VMDATALREAD	$2139
.define VMDATAHREAD	$213a
.define CGDATAREAD	$213b
.define OPHCT		$213c
.define OPVCT		$213d
.define STAT77		$213e
.define STAT78		$213f

.define WMDATA		$2180
.define WMADDL		$2181
.define WMADDM		$2182
.define WMADDH		$2183

.define JOYSER0		$4016
.define JOYSER1		$4017

.define NMITIMEN	$4200

.define WRMPYA		$4202
.define WRMPYB		$4203
.define WRDIVL		$4204
.define WRDIVH		$4205
.define WRDIVB		$4206

.define HTIMEL		$4207
.define HTIMEH		$4208
.define VTIMEL		$4209
.define VTIMEH		$420a
.define MDMAEN		$420b
.define HDMAEN		$420c
.define MEMSEL		$420d



Start:
    InitSNES

    rep #$10
    sep #$20
    
    stz PalNum

    LoadPalette BG_Palette, 0, 14
    LoadBlockToVRAM Tiles, $0000, TilesEnd-Tiles
    
    lda #$80
    sta $2115
    ldx #$0400
    stx $2116

    ldy #0
-   
    ldx #$1
    stx $2118
    iny
    cpy #$400
    bne -

	lda.b #:TestCHR
	sta.b ACK_InputBank
	ldx.w #TestCHR
	stx.w ACK_InputOffset
	



    jsr SetupVideo
    
    lda #$80
    sta $4200       ; Enable NMI

Infinity:
    Stall

    lda PalNum
    clc
    adc #$04
    and #$0C        ; If > palette starting color > 24 (00011100), make 0
    sta PalNum

_done:
    JMP Infinity

;============================================================================
VBlank:
    rep #$10        ; X/Y=16 bits
    sep #$20        ; A/mem=8 bit    
    
    stz $2115       ; Setup VRAM
    ldx #$0400
    stx $2116       ; Set VRAM address
    lda PalNum
    sta $2119       ; Write to VRAM

    lda $4210       ; Clear NMI flag
    
    RTI
;============================================================================

;============================================================================
; SetupVideo -- Sets up the video mode and tile-related registers
;----------------------------------------------------------------------------
; In: None
;----------------------------------------------------------------------------
; Out: None
;----------------------------------------------------------------------------
SetupVideo:

    lda #$00
    sta $2105           ; Set Video mode 1, 8x8 tiles, 16 color BG1/2, 4 color BG3

    lda #$04            ; Set BG1's Tile Map offset to $0400 (Word address)
    sta $2107           ; And the Tile Map size to 32x32

    stz $210B           ; Set BG1's Character VRAM offset to $0000 (word address)

    lda #$01            ; Enable BG1
    sta $212C

    lda #$0F
    sta $2100           ; Turn on screen, full Brightness

    rts

	.include "deacksnes.asm"

.ENDS
;============================================================================

.BANK 1 SLOT 0
.ORG 0
.SECTION "CharacterData"

Tiles:
    .DW $0000, $0000, $0000, $0000, $0000, $0000, $0000, $0000
    .DB $00,$00,$24,$00,$24,$00,$24,$00
    .DB $00,$00,$81,$00,$FF,$00,$00,$00
TilesEnd:

BG_Palette:
    .DB $00, $00, $FF, $03
    .DW $0000, $0000, $0000
    .DB $1F, $00
    .DW $0000, $0000, $0000
    .DB $E0, $5D
    .DW $0000, $0000, $0000
    .DB $E0, $02

TestCHR:
    .incbin "battlekid.ack"
TestCHREnd:

.ENDS

